package huawei;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 学生方阵
 */
public class Q99 {

//    static char[][] zhen = {{'F', 'M', 'M', 'F'},{'F', 'M', 'M', 'F'},{'F', 'F', 'F', 'M'}};
    static char[][] zhen = {{'F', 'F', 'F', 'F'},{'F', 'F', 'F', 'F'},{'F', 'F', 'F', 'F'}};

    static int[][] hen = {{0, 1},{0, -1}}; // 横
    static int[][] shu = {{1, 0},{-1, 0}}; // 竖
    static int[][] dui1 = {{1, 1},{-1, -1}};
    static int[][] dui2 = {{-1, 1},{1, -1}};

    static List<Set<String>> henList = new ArrayList<>();
    static List<Set<String>> shuList = new ArrayList<>();
    static List<Set<String>> dui1List = new ArrayList<>();
    static List<Set<String>> dui2List = new ArrayList<>();

    public static void main(String[] args) {
        for (int i = 0; i < zhen.length; i++) {
            for (int j = 0; j < zhen[i].length; j++) {
                if (zhen[i][j] == 'M') {
                    Set<String> henSet = new HashSet<>();
                    dfs(i, j, 0, henSet); // 横
                    if (henSet.size() > 0) {
                        henList.add(henSet);
                    }
                    Set<String> shuSet = new HashSet<>();
                    dfs(i, j, 1, shuSet); // 竖
                    if (shuSet.size() > 0) {
                        shuList.add(shuSet);
                    }
                    Set<String> dui1Set = new HashSet<>();
                    dfs(i, j, 2, dui1Set); // 斜1
                    if (dui1Set.size() > 0) {
                        dui1List.add(dui1Set);
                    }
                    Set<String> dui2Set = new HashSet<>();
                    dfs(i, j, 3, dui2Set); // 斜2
                    if (dui2Set.size() > 0) {
                        dui2List.add(dui2Set);
                    }
                }
            }
        }

//        System.out.println(henList);
//        System.out.println(shuList);
//        System.out.println(dui1List);
//        System.out.println(dui2List);

        List<Set<String>> all = new ArrayList<>();
        all.addAll(henList);
        all.addAll(shuList);
        all.addAll(dui1List);
        all.addAll(dui2List);

        all.sort((e1, e2) -> e2.size() - e1.size() );

//        System.out.println(all);
        if (!all.isEmpty()) {
            System.out.println(all.get(0).size());
        } else {
            System.out.println(0);
        }
    }

    public static void dfs(int i, int j, int direction, Set<String> set) {
        if (!(i >= 0 && j >= 0 && i < zhen.length && j < zhen[i].length && zhen[i][j] == 'M' && !set.contains(i + "_" + j))) {return;}

        if (direction == 0) {
            for (Set<String> henSet : henList) {
                if (henSet.contains(i + "_" + j)) {
                    set.clear();
                    return;
                }
            }
        }

        if (direction == 1) {
            for (Set<String> shuSet : shuList) {
                if (shuSet.contains(i + "_" + j)) {
                    set.clear();
                    return;
                }
            }
        }

        if (direction == 2) {
            for (Set<String> duiSet : dui1List) {
                if (duiSet.contains(i + "_" + j)) {
                    set.clear();
                    return;
                }
            }
        }

        if (direction == 3) {
            for (Set<String> duiSet : dui2List) {
                if (duiSet.contains(i + "_" + j)) {
                    set.clear();
                    return;
                }
            }
        }

        set.add(i + "_" + j);
        if (direction == 0) {
            for (int[] h : hen) {
                dfs(i + h[0], j + h[1], direction, set);
            }
        }
        if (direction == 1) {
            for (int[] s : shu) {
                dfs(i + s[0], j + s[1], direction, set);
            }
        }
        if (direction == 2) {
            for (int[] d : dui1) {
                dfs(i + d[0], j + d[1], direction, set);
            }
        }
        if (direction == 3) {
            for (int[] d : dui2) {
                dfs(i + d[0], j + d[1], direction, set);
            }
        }
    }
}
